using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using AutoMapper;
using ShopForWeb.Models;


namespace ShopForWeb.Service
{
  /// <summary>
  /// 商城业务层的基类
  /// </summary>
  public class BaseService
  {
    /// <summary>
    /// EFCore DbContext
    /// </summary>
    protected readonly ShopDbContext _context;

    /// <summary>
    /// 站点配置文件（appsetttings.json）的读取器
    /// </summary>
    protected readonly IConfiguration _configration;

    /// <summary>
    /// AutoMapper
    /// </summary>
    protected readonly IMapper _mapper;

    public BaseService(ShopDbContext context, IConfiguration configration, IMapper mapper)
    {
      _context = context;
      _configration = configration;
      _mapper = mapper;
    }

    /// <summary>
    /// 读写分离-主库
    /// </summary>
    protected void UseMasterDB()
    {
      _context.Database.SetConnectionString(_configration.GetConnectionString("ShopMasterDB"));
    }

    /// <summary>
    /// 读写分离-从库
    /// </summary>
    protected void UseSlaveDB()
    {
      _context.Database.SetConnectionString(_configration.GetConnectionString("ShopSlaveDB"));
    }

  }
}
